package redis

import (
	"fmt"
	"github.com/gomodule/redigo/redis"
)

func Store(key string, value string) {
	c, err, done := InitRedis()
	if done {
		return
	}
	_, errr := c.Do("Set", key, value)
	if err != nil {
		fmt.Println(errr)
		return
	}
	defer c.Close()
}

func InitRedis() (redis.Conn, error, bool) {
	c, err := redis.Dial("tcp", "localhost:6379")
	if err != nil {
		fmt.Print(err)
		return nil, nil, true
	}
	return c, err, false
}
func Get(key string) string {
	c, err, done := InitRedis()
	if done {
		return "err"
	}
	res, errr := redis.String(c.Do("Get", key))
	if err != nil {
		fmt.Println(errr)
		return "err"
	}
	defer c.Close()
	return res
}
func StoreList(key string, value string) {
	c, err, done := InitRedis()
	if done {
		return
	}
	_, errr := c.Do("LPUSH", key, value)
	if err != nil {
		fmt.Println(errr)
		return
	}
	defer c.Close()
}
func GetList(key string) []string {
	c, _, _ := InitRedis()

	res, errr := redis.Strings(c.Do("LRANGE", key, 0, 5))
	if errr != nil {
		fmt.Println(errr)
	}
	defer c.Close()
	return res
}
